package businesslogic.userBl;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.util.ArrayList;

import po.userPO.StudentPO;
import po.userPO.TeacherPO;
import po.userPO.UserPO;
import vo.userVO.StudentVO;
import vo.userVO.TeacherVO;
import businesslogicservice.userblservice.UniversityDeanBLService;
import dataservice.userDataService.UserDatabaseService;


public class UniversityDean extends User implements UniversityDeanBLService{

	private static final String ADDRESS_STRING="localhost";
	
	public UniversityDean(String UserID) {
		super(UserID);
		// TODO Auto-generated constructor stub
	}

	

	
	
	@Override
	public ArrayList<StudentVO> getStudentList(String departmentID)throws Exception {
		ArrayList<StudentVO> studentList=new ArrayList<StudentVO>();
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{
			
			UserDatabaseService userDataService = (UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
		
			
			ArrayList<UserPO> findResult=userDataService.findAllByDepartment(0,departmentID);
			
			for(UserPO user:findResult){
				StudentPO userpo=(StudentPO)user;
				StudentVO student=new StudentVO(userpo.getId(),userpo.getName(),userpo.getDepartmentId(),userpo.getGrade());
				studentList.add(student);
				
			}
			
			return studentList;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
			
		}
	}

	

	@Override
	public ArrayList<TeacherVO> getTeacherList(String departmentID)throws Exception{
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		ArrayList<TeacherVO> teacherList=new ArrayList<TeacherVO>();
		try{
			
			UserDatabaseService userDataService = (UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
		
			
			ArrayList<UserPO> findResult=userDataService.findAllByDepartment(UserPO.TEACHER,departmentID);
			
			for(UserPO user:findResult){
				TeacherPO userpo=(TeacherPO)user;
				TeacherVO teacher=new TeacherVO(userpo.getId(),userpo.getName(),userpo.getDepartmentId());
				teacherList.add(teacher);
				
			}
			
			return teacherList;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
			
		}
	}
	
	public ArrayList<TeacherVO> getTeacherList()throws Exception{
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		ArrayList<TeacherVO> teacherList=new ArrayList<TeacherVO>();
		try{
			
			UserDatabaseService userDataService = (UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
		
			
			ArrayList<UserPO> findResult=userDataService.findAll(1);
			
			for(UserPO user:findResult){
				TeacherPO userpo=(TeacherPO)user;
				TeacherVO teacher=new TeacherVO(userpo.getId(),userpo.getName(),userpo.getDepartmentId());
				teacherList.add(teacher);
				
			}
			
			return teacherList;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
			
		}
		
		
	}
	
	public ArrayList<StudentVO> getStudentList()throws Exception{
		ArrayList<StudentVO> studentList=new ArrayList<StudentVO>();
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{
			
			UserDatabaseService userDataService = (UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
		
			
			ArrayList<UserPO> findResult=userDataService.findAll(0);
			
			for(UserPO user:findResult){
				StudentPO userpo=(StudentPO)user;
				StudentVO student=new StudentVO(userpo.getId(),userpo.getName(),userpo.getDepartmentId(),userpo.getGrade());
				studentList.add(student);
				
			}
			
			return studentList;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
			
		}
	}

	

}
 